<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
export default {
  provide() {
    return {
      // 将表单实例传递给后代
      form: this,
    };
  },
  name: "FormItem",
  props: {
    model: {
      type: Object,
      required: true,
    },
    rules: Object,
  },
  data() {
    return {};
  },
  created() {
    console.log(this.rules);
  },
  methods: {
    validate(callback) {
      // 全局校验
      // 遍历FormItem，执行validate方法
      const tasks = this.$children
        .filter((item) => item.prop)
        .map((item) => item.validate());
      Promise.all(tasks)
        .then(() => callback(true))
        .catch(() => {
          callback(false);
        });
    },
  },
};
</script>

<style>
</style>